<?php

class Model_HmNewsCategory extends Zend_Db_Table_Abstract
{
	protected $_name = 'hm_news_category';
	protected $_primary = 'hm_news_id';
	
	protected $_referenceMap = array(
        'Model_HmNews' => array(
            'columns'       => array('hm_news_id'),
            'refTableClass' => 'Model_HmNews',
            'refColumns'    => array('id'),
			'onDelete'      => self::CASCADE
        ),
        'Model_HmNewsCategoryDescr' => array(
            'columns'       => array('hm_news_category_descr_id'),
            'refTableClass' => 'Model_HmNewsCategoryDescr',
            'refColumns'    => array('id'),
			'onDelete'      => self::CASCADE
        )
    );
	
	public function getLink($id)
	{
		$query = $this->select()
							->setIntegrityCheck(false)
							->from($this)
							->join('hm_news_category_descr', 'hm_news_category_descr.id = hm_news_category.hm_news_category_descr_id')
							->where('hm_news_id = '.(int)$id);
		$row = $this->fetchAll($query);
		if(!$row) {
			throw new Exception('Could not fetch records.');
		}
		return $row->toArray();
	}
	
	public function getRow($id)
	{
		$row = $this->fetchRow('id = '.(int)$id);
		if(!$row) {
			throw new Exception('Could not fetch row '.(int)$id);
		}
		return $row->toArray();
	}
	
	public function addRow($data)
	{
		$row = $this->createRow($data);
		$row->save();
	}
	
	public function deleteRow($id, $category)
	{
		$where = 'hm_news_id = '.(int)$id.' AND hm_news_category_descr_id = '.(int)$category;
		$row = $this->delete($where);
		if(!$row) {
			throw new Exception('Could not delete row '.(int)$id);
		}
		return true;
	}
}